<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="器件名称" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入器件名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="物料编号" prop="materialNumber">
        <el-input
          v-model="queryParams.materialNumber"
          placeholder="请输入物料编号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
<!--      <el-form-item label="型号/规格" prop="model">-->
<!--        <el-input-->
<!--          v-model="queryParams.model"-->
<!--          placeholder="请输入型号/规格"-->
<!--          clearable-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="封装" prop="encapsulation">-->
<!--        <el-input-->
<!--          v-model="queryParams.encapsulation"-->
<!--          placeholder="请输入封装"-->
<!--          clearable-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="数量" prop="quantity">-->
<!--        <el-input-->
<!--          v-model="queryParams.quantity"-->
<!--          placeholder="请输入数量"-->
<!--          clearable-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['materials:backweldMaterial:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['materials:backweldMaterial:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['materials:backweldMaterial:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['materials:backweldMaterial:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="backweldMaterialList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
<!--      <el-table-column label="后焊材料id" align="center" prop="materialId" />-->
      <el-table-column label="器件名称" align="center" prop="name" />
      <el-table-column label="物料编号" align="center" prop="materialNumber" />
      <el-table-column label="型号/规格" align="center" prop="model" />
      <el-table-column label="封装" align="center" prop="encapsulation" />
      <el-table-column label="数量" align="center" prop="quantity" />
      <el-table-column label="备注" align="center" prop="remark" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-shopping-cart-2"
            @click="outStorage(scope.row)"
            v-hasPermi="['materials:backweldMaterial:out']"
          >出库</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-shopping-cart-full"
            @click="putStorage(scope.row)"
            v-hasPermi="['materials:backweldMaterial:put']"
          >入库</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['materials:backweldMaterial:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['materials:backweldMaterial:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 出入库原材料对话框 -->
    <el-dialog :title="title" :visible.sync="openInventory" width="500px" append-to-body>
      <el-form ref="inventory" :model="inventory" :rules="rules" label-width="80px">
        <el-form-item label="库存" prop="quantity">
          <el-input v-model="inventory.quantity" :disabled = "true"/>
        </el-form-item>
        <el-form-item label="入库" prop="variable" v-if="flash">
          <el-input v-model="inventory.variable" placeholder="请输入入库数量" />
        </el-form-item>
        <el-form-item label="出库" prop="remove" v-if="!flash">
          <el-input v-model="inventory.remove" placeholder="请输入出库库数量" />
        </el-form-item>
        <el-form-item label="备注" prop="infoRemark">
          <el-input type="textarea" v-model="inventory.infoRemark" placeholder="请输入备注" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitInventory">操作</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 添加或修改后焊材料对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="后焊器件" prop="materialId" >
          <el-select v-model="form.materialId" filterable placeholder="请选择" :disabled = "first">
            <el-option
              v-for="(item, index) in materials"
              :key="item.materialId "
              :label="item.name + ' - ' + materialNumbers[index]+ ' - ' + models[index]"
              :value="item.materialId">
            </el-option>
          </el-select>
        </el-form-item>
<!--        <el-form-item label="器件名称" prop="name">-->
<!--          <el-input v-model="form.name" placeholder="请输入器件名称" />-->
<!--        </el-form-item>-->
<!--        <el-form-item label="物料编号" prop="materialNumber">-->
<!--          <el-input v-model="form.materialNumber" placeholder="请输入物料编号" />-->
<!--        </el-form-item>-->
<!--        <el-form-item label="型号/规格" prop="model">-->
<!--          <el-input v-model="form.model" placeholder="请输入型号/规格" />-->
<!--        </el-form-item>-->
<!--        <el-form-item label="封装" prop="encapsulation">-->
<!--          <el-input v-model="form.encapsulation" placeholder="请输入封装" />-->
<!--        </el-form-item>-->
<!--        <el-form-item label="数量" prop="quantity">-->
<!--          <el-input v-model="form.quantity" placeholder="请输入数量" />-->
<!--        </el-form-item>-->
        <el-form-item label="备注" prop="remark">
          <el-input type="textarea" v-model="form.remark" placeholder="请输入备注" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listBackweldMaterial,
  getBackweldMaterial,
  delBackweldMaterial,
  addBackweldMaterial,
  updateBackweldMaterial,
  updateBackWeldMaterialQuantity
} from "@/api/materials/backweldMaterial";
import {getMaterial, listMaterial, updateMaterialQuantity} from "@/api/materials/material";

export default {
  name: "BackweldMaterial",
  data() {
    return {
      first: false,
      flash : false,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      materials:[],
      materialNumbers:[],
      models:[],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 后焊材料表格数据
      backweldMaterialList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      openInventory: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        name: null,
        materialNumber: null,
        model: null,
        encapsulation: null,
        quantity: null,
      },
      // 表单参数
      form: {},
      inventory:{},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
    this.getMaterialsList();
  },
  methods: {
    /** 查询后焊材料列表 */
    getList() {
      this.loading = true;
      listBackweldMaterial(this.queryParams).then(response => {
        this.backweldMaterialList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    /** 查询原材料列表 */
    getMaterialsList() {
      this.loading = true;
      this.queryParams.pageSize = 1000;//查询列表大小
      listMaterial(this.queryParams).then(response => {
        this.materials = response.rows;
        this.materialNumbers = response.rows.map(product => product.materialNumber);
        this.models = response.rows.map(product => product.model);
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.openInventory = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        materialId: null,
        name: null,
        materialNumber: null,
        model: null,
        encapsulation: null,
        quantity: null,
        remark: null
      };
      this.resetForm("form");
      this.inventory = {
        materialId: null,
        name: null,
        materialNumber: null,
        model: null,
        encapsulation: null,
        quantity: null,
        remark: null,
        variable:0,
        remove: 0,
        infoRemark: null,
      };
      this.resetForm("inventory");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.materialId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.first = false;
      this.open = true;
      this.title = "添加后焊材料";
      this.form.remark = null;
    },
    /** 入库按钮操作 */
    putStorage(row) {
      this.reset();
      this.flash = true;
      const materialId = row.materialId
      getBackweldMaterial(materialId).then(response => {
        this.inventory = response.data;
        this.inventory.remove = 0;
        this.openInventory = true;
        this.title = "后焊器件入库";
      });
    },
    /** 出库按钮操作 */
    outStorage(row) {
      this.reset();
      this.flash = false;
      const materialId = row.materialId
      getBackweldMaterial(materialId).then(response => {
        this.inventory = response.data;
        this.inventory.variable = 0;
        this.openInventory = true;
        this.title = "后焊器件出库";
      });
    },
    /** 出入库按钮 */
    submitInventory() {
      this.$refs["inventory"].validate(valid => {
        if (valid) {
          if (this.inventory.materialId != null) {
            updateMaterialQuantity(this.inventory).then(response => {
              this.$modal.msgSuccess("操作成功");
              this.openInventory = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const materialId = row.materialId || this.ids
      this.first = true;
      getBackweldMaterial(materialId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改后焊材料";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          // if (this.form.materialId != null) {
          //   updateBackweldMaterial(this.form).then(response => {
          //     this.$modal.msgSuccess("修改成功");
          //     this.open = false;
          //     this.getList();
          //   });
          // } else {
            addBackweldMaterial(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          // }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const materialIds = row.materialId || this.ids;
      this.$modal.confirm('是否确认删除后焊材料编号为"' + materialIds + '"的数据项？').then(function() {
        return delBackweldMaterial(materialIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('materials/backweldMaterial/export', {
        ...this.queryParams
      }, `backweldMaterial_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
